import datetime
import os.path
import sys

from PIL import ImageGrab
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager


class Driver:
    driver = ""

    def __init__(self):
        # options = webdriver.ChromeOptions()
        # service = Service(ChromeDriverManager().install())
        # self.driver = webdriver.Chrome(service = service, options = options)
        options = webdriver.EdgeOptions()
        service = Service(EdgeChromiumDriverManager().install())
        self.driver = webdriver.Edge(service=service, options=options)

    def getScreenShort(self):
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        if not os.path.exists("../images/" + dirname):
            os.mkdir("../images/" + dirname)
        filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S") +".png"
        self.driver.save_screenshot("../images/" + dirname + "/" + filename)

    def get_alert_image(self):
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        if not os.path.exists("../images/" + dirname):
            os.mkdir("../images/" + dirname)
        filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S") +".png"
        screenshot = ImageGrab.grab()
        screenshot.save("../images/" + dirname + "/" + filename)


ChatDriver = Driver()
